package com.bestcem.xm.txtanls.dao.impl.mysql;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.constant.Constants;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.txtanls.dao.ViewpointDao;
import com.bestcem.xm.txtanls.dao.mapper.ViewpointMapper;
import com.bestcem.xm.txtanls.entity.mysql.ViewpointEntity;
import com.bestcem.xm.txtanls.entity.pojo.ViewpointDO;
import com.bestcem.xm.txtanls.enums.ViewpointTypeEnum;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static org.apache.logging.log4j.util.Strings.EMPTY;

/**
 * @author wangyf
 * @version v1.0
 * @date 2021/9/27 14:39
 */
@Repository
public class ViewpointDaoImpl implements ViewpointDao {

    @Resource
    private ViewpointMapper viewpointMapper;

    /**
     * 根据id和类型查询观点
     *
     * @param id 观点id
     * @return Viewpoint 观点
     * @author jy.zhao
     * @date 2021/7/28 12:21
     **/
    @Override
    public ViewpointDO getById(String id) {
        ViewpointEntity entity = viewpointMapper.findById(id);
        return toDO(entity);
    }

    /**
     * 根据项目id查询观点列表
     *
     * @param projectId 项目id
     * @return List<ViewpointEntity> 观点列表
     * @author jy.zhao
     * @date 2021/5/13 13:56
     **/
    @Override
    public List<ViewpointDO> listViewpoints(String projectId) {
        List<ViewpointEntity> entities = viewpointMapper.findByProjectId(projectId);
        if (CollUtil.isNotEmpty(entities)) {
            return entities.stream().map(entity -> toDO(entity)).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    /**
     * 查询项目下所有的子观点
     *
     * @param projectId 项目id
     * @return Project all children viewpoint
     * @author gy.liu
     * @date 2021/5/11 14:20
     **/
    @Override
    public List<ViewpointDO> findAllChildrenViewPoint(String projectId) {
        List<ViewpointEntity> entities =
                viewpointMapper.findByProjectIdAndType(projectId, ViewpointTypeEnum.CHILDREN.getIndex());
        if (CollUtil.isNotEmpty(entities)) {
            return entities.stream().map(entity -> toDO(entity)).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    /**
     * 插入父观点
     *
     * @param viewContent 观点翻译内容
     * @param projectId   文本分析项目id
     * @param industryId  行业id
     * @return Created parent viewpoint
     * @author XiaoYunSong <ys.xiao@idiaoyan.cn>
     * @date 2021/5/12 18:07
     */
    @Override
    public String insertParentViewpoint(String viewContent, String projectId, String industryId) {
        ViewpointEntity entity = new ViewpointEntity();
        entity.setId(ObjectId.next());
        entity.setViewContent(viewContent);
        entity.setRowContent(EMPTY);
        entity.setProjectId(projectId);
        entity.setGroupId(EMPTY);
        entity.setIndustryId(industryId);
        entity.setType(ViewpointTypeEnum.PARENT.getIndex());
        entity.setCreateTime(DateUtil.getCommonDate());
        entity.setUpdateTime(DateUtil.getCommonDate());
        viewpointMapper.create(entity);
        return entity.getId();
    }

    /**
     * 插入子观点
     *
     * @param viewContent 观点翻译内容
     * @param rowContent  观点源内容
     * @param projectId   文本分析项目id
     * @param parentId    父观点id
     * @param industryId  行业id
     * @return Created parent viewpoint
     * @author XiaoYunSong <ys.xiao@idiaoyan.cn>
     * @date 2021/5/12 18:07
     */
    @Override
    public void insertChildrenViewpoint(String viewContent, String rowContent, String projectId, String parentId, String industryId) {
        ViewpointEntity entity = new ViewpointEntity();
        entity.setId(ObjectId.next());
        entity.setViewContent(viewContent);
        entity.setRowContent(rowContent);
        entity.setProjectId(projectId);
        entity.setGroupId(parentId);
        entity.setIndustryId(industryId);
        entity.setType(ViewpointTypeEnum.CHILDREN.getIndex());
        entity.setCreateTime(DateUtil.getCommonDate());
        entity.setUpdateTime(DateUtil.getCommonDate());
        viewpointMapper.create(entity);
    }

    /**
     * 删除分析项目下的全部观点
     *
     * @param projectId 项目id
     * @return The result of a delete operation
     * @author XiaoYunSong <ys.xiao@idiaoyan.cn>
     * @date 2021/5/12 18:07
     */
    @Override
    public boolean deleteProjectAllViewpoint(String projectId) {
        return viewpointMapper.deleteByProjectId(projectId) > Constants.INT_ZERO;
    }

    @Override
    public ViewpointDO insertViewpoint(ViewpointDO viewpoint) {
        viewpoint.setId(ObjectId.next().toString());
        ViewpointEntity viewpointEntity = toEntity(viewpoint);
        viewpointMapper.create(viewpointEntity);
        return viewpoint;


    }

    @Override
    public List<ViewpointDO> listChildrenViewpoint(String viewpointId, String projectId) {

        List<ViewpointEntity> viewpointEntities = viewpointMapper.listChildrenViewpoint(viewpointId, projectId);

        return toDoList(viewpointEntities);

    }


    private List<ViewpointDO> toDoList(List<ViewpointEntity> entities) {
        if (CollUtil.isEmpty(entities)) {
            return Collections.emptyList();
        }
        ArrayList<ViewpointDO> viewpointDOS = new ArrayList<>();
        for (ViewpointEntity entity : entities) {
            viewpointDOS.add(toDO(entity));
        }

        return viewpointDOS;
    }

    /**
     * ViewpointEntity to ViewpointDO
     * `
     *
     * @param entity
     * @return
     */
    private ViewpointDO toDO(ViewpointEntity entity) {
        ViewpointDO viewpointDO = new ViewpointDO();
        viewpointDO.setCreateId(entity.getCreateId());
        viewpointDO.setCreateTime(entity.getCreateTime());
        viewpointDO.setGroupId(entity.getGroupId());
        viewpointDO.setId(entity.getId());
        viewpointDO.setIndustryId(entity.getIndustryId());
        viewpointDO.setProjectId(entity.getProjectId());
        viewpointDO.setRowContent(entity.getRowContent());
        viewpointDO.setType(entity.getType());
        viewpointDO.setUpdateId(entity.getUpdateId());
        viewpointDO.setUpdateTime(entity.getUpdateTime());
        viewpointDO.setViewContent(entity.getViewContent());
        viewpointDO.setSample(entity.getSample());
        return viewpointDO;
    }

    /**
     * ViewpointDO to ViewpointEntity
     * `
     *
     * @param entity
     * @return
     */
    private ViewpointEntity toEntity(ViewpointDO entity) {
        ViewpointEntity viewpointEntity = new ViewpointEntity();
        viewpointEntity.setCreateId(entity.getCreateId());
        viewpointEntity.setCreateTime(entity.getCreateTime());
        viewpointEntity.setGroupId(entity.getGroupId());
        viewpointEntity.setId(entity.getId());
        viewpointEntity.setIndustryId(entity.getIndustryId());
        viewpointEntity.setProjectId(entity.getProjectId());
        viewpointEntity.setRowContent(entity.getRowContent());
        viewpointEntity.setType(entity.getType());
        viewpointEntity.setUpdateId(entity.getUpdateId());
        viewpointEntity.setUpdateTime(entity.getUpdateTime());
        viewpointEntity.setViewContent(entity.getViewContent());
        viewpointEntity.setSample(entity.getSample());
        return viewpointEntity;
    }
}
